package com.beiye.drivertransport.safelearn;

import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.bean.LoginUserBean;
import com.beiye.drivertransport.bean.MineUseBean;
import com.beiye.drivertransport.bean.UserBean;
import com.beiye.drivertransport.http.Login;
import com.beiye.drivertransport.utils.LogUtils;
import com.beiye.drivertransport.utils.UserManger;
import com.githang.statusbar.StatusBarCompat;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class StubActivity extends TwoBaseAty {


    ImageView acStubIvHeadImg;

    TextView acStubTvCertNum;

    TextView acStubTvName;

    TextView acStubTvSex;

    TextView acStubTvIdcNo;

    TextView acStubTvJob;

    TextView acStubTvOrgName;

    TextView acStubTvOrgType;

    TextView acStubTvCertDate;

    ImageView acStubIvSignImg;

    TextView acStubTvDate;

    TextView acStubTvChName;

    ImageView imgMainBack;

    private int sn;
    private String name;
    private String sex;
    private String idcNo;
    private String job;
    private String orgName;
    private String orgType;
    private String studyPlan;
    private long endDate;
    private String chName;
    private String orgId;

    private static final int GET_USERINFO = 1;
    private static final int GETUSER_ORGINFO = 2;

    @Override
    public int getLayoutId() {
        return R.layout.activity_stub;
    }

    @Override
    public void initData() {
        acStubIvHeadImg = (ImageView) findViewById(R.id.ac_stub_iv_headImg);
        acStubTvCertNum = (TextView) findViewById(R.id.ac_stub_tv_certNum);
        acStubTvName = (TextView) findViewById(R.id.ac_stub_tv_name);
        acStubTvSex = (TextView) findViewById(R.id.ac_stub_tv_sex);
        acStubTvIdcNo = (TextView) findViewById(R.id.ac_stub_tv_idcNo);
        acStubTvJob = (TextView) findViewById(R.id.ac_stub_tv_job);
        acStubTvOrgName = (TextView) findViewById(R.id.ac_stub_tv_orgName);
        acStubTvOrgType = (TextView) findViewById(R.id.ac_stub_tv_orgType);
        acStubTvCertDate = (TextView) findViewById(R.id.ac_stub_tv_certDate);
        acStubIvSignImg = (ImageView) findViewById(R.id.ac_stub_iv_signImg);
        acStubTvDate = (TextView) findViewById(R.id.ac_stub_tv_date);
        acStubTvChName = (TextView) findViewById(R.id.ac_stub_tv_chName);
        imgMainBack = (ImageView) findViewById(R.id.img_main_back);
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));

        Bundle bundle = getIntent().getExtras();
        sn = bundle.getInt("sn");
        chName = bundle.getString("chName");
        endDate = bundle.getLong("endDate");
        orgId=bundle.getString("orgId");
        String signPic = bundle.getString("signPicUrl");

        acStubTvChName.setText(chName);
        acStubTvCertNum.setText("NO." + getTime(new Date(endDate), "yyyyMMddHHmmss") + sn);
        acStubTvCertDate.setText(getTime(new Date(endDate), "yyyy年MM月dd日"));
        acStubTvDate.setText(getTime(new Date(endDate), "yyyy年MM月dd日"));
        Picasso.with(this).load(Uri.parse(signPic)).into(acStubIvSignImg);
    }

    @Override
    public void requestData() {
        getUserInfo();
        getUserOrgInfo();
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        super.onSuccess(result, call, response, what);
        if (what == GET_USERINFO) {
            MineUseBean mineUseBean = JSON.parseObject(result, MineUseBean.class);
            MineUseBean.DataBean data = mineUseBean.getData();
            idcNo = data.getIdcNo();
            String photoUrl = data.getPhotoUrl();
            sex = data.getSex() == null ? "男" : (data.getSex().equals("M") ? "男" : "女");

            acStubTvIdcNo.setText(idcNo);
            acStubTvSex.setText(sex);
            acStubTvName.setText(data.getUserName());
            Picasso.with(this).load(Uri.parse(photoUrl)).placeholder(R.mipmap.my_head).into(acStubIvHeadImg);
        }else if(what==GETUSER_ORGINFO){
            LogUtils.e("测试", "onSuccess: "+result );
            UserBean userBean = JSON.parseObject(result, UserBean.class);
            List<LoginUserBean.DataBean.CurUserOrgBean> rows = userBean.getRows();
            LogUtils.e("测试", "onSuccess: "+rows.size() );
            LogUtils.e("测试", "onSuccess: "+orgId);
            for(int i=0;i<rows.size();i++){
                LogUtils.e("测试", "onSuccess: "+rows.get(i).getOrgId()+"  "+orgId );
                if(orgId.equals(rows.get(i).getOrgId())){
                    int ftId=rows.get(i).getFtId();
                    if(ftId==100001){
                        acStubTvOrgType.setText("道路城市客运");
                        acStubTvJob.setText("城市客运驾驶员");
                    }else if(ftId==100002){
                        acStubTvOrgType.setText("道路普通货运");
                        acStubTvJob.setText("普通货运驾驶员");
                    }else if(ftId==100003){
                        acStubTvOrgType.setText("道路危货运输");
                        acStubTvJob.setText("危货运输驾驶员");
                    }else if(ftId==100004){
                        acStubTvOrgType.setText("道路旅客运输");
                        acStubTvJob.setText("旅客运输驾驶员");
                    }
                }
            }
        }
    }


    public void onClick(View view) {
        finish();
    }

    //自定义方法
    private String getTime(Date date, String form) {
        SimpleDateFormat sf = new SimpleDateFormat(form);
        String data = sf.format(date);
        return data;
    }

    //网络请求

    private void getUserInfo() { //获取用户信息
        LoginUserBean userInfo = UserManger.getUserInfo();
        String userId = userInfo.getData().getUserId();
        new Login().getUser(userId, this, GET_USERINFO);
    }

    private void getUserOrgInfo() {//获取企业列表
        LoginUserBean userInfo = UserManger.getUserInfo();
        String userId = userInfo.getData().getUserId();
        new Login().getUseData(userId, 1, this, GETUSER_ORGINFO);
    }
}
